package com.tsd.system.service.impl;

import com.tsd.CommonMsgNameConstants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysExtSysUserMapper;
import com.tsd.system.entity.SysExtSysUser;
import com.tsd.system.entity.SysExtSysUserExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserBind3rdExt;
import com.tsd.system.service.SysExtSysUserService;
import com.tsd.system.service.SysUserBind3rdService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysExtSysUser 服务实现层
 *
 * @author huawei
 * @date 2021/07/18 10:53:52
 */
@Service
public class SysExtSysUserServiceImpl extends BaseServiceImpl implements SysExtSysUserService {

    @Resource
    private SysExtSysUserMapper sysExtSysUserMapper;
    @Resource
    private SysUserBind3rdService sysUserBind3rdService;

    @Override
    public SysExtSysUserExt queryBySid(String sid) throws Exception {
        return sysExtSysUserMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysExtSysUser(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            SysExtSysUser old = sysExtSysUserMapper.selectBySid(sid);
            super.checkEmpty(old, CommonMsgNameConstants.ERROR_MSG_RECORD_NOT_EXISTS);
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            sysExtSysUserMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveSysExtSysUser(SysExtSysUserExt record, SysUser opUser) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("ext_sys_name", record.getExt_sys_name());
        params.put("app_id", record.getApp_id());
        params.put("open_id", record.getOpen_id());
        SysExtSysUserExt old = queryByExtSysNameOpenId(params);
        if (old != null) {
            record.setId(old.getId());
            record.setSid(old.getSid());
        }
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, TYPE_CREATE);
            sysExtSysUserMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, TYPE_UPDATE);
            record.setDr(0);
            sysExtSysUserMapper.updateByPrimaryKeySelective(record);
        }
        if (record.getBindList() != null && record.getBindList().size() > 0) {
            for (SysUserBind3rdExt t : record.getBindList()) {
                t.setExt_sys_sid(record.getSid());
                sysUserBind3rdService.saveSysUserBind3rd(t, opUser);
            }
        }
    }

    @Override
    public List<SysExtSysUserExt> findSysExtSysUsers(Map<String, Object> params, PageBean page) throws Exception {
        return sysExtSysUserMapper.selectByPage(params, page);
    }

    @Override
    public List<SysExtSysUserExt> findSysExtSysUsers(Map<String, Object> params) throws Exception {
        return sysExtSysUserMapper.selectByParams(params);
    }

    @Override
    public SysExtSysUserExt queryByExtSysNameOpenId(Map<String, Object> params) throws Exception {
        return sysExtSysUserMapper.selectByExtSysNameOpenId(params);
    }

    @Override
    public List<SysExtSysUserExt> findSysExtSysUsersByUserSid(String user_sid) throws Exception {
        return sysExtSysUserMapper.selectByUserSid(user_sid);
    }
}